<?php
namespace Site\Controller;
class TagsController extends CommonController {
    public function index() {
        $this->assign($this->getPageList(M('SiteTags'),array('sid'=>$this->sid)));
        $this->display();
    }

    public function add() {
        if(IS_POST) {
            D('SiteTags','Mysql')->addSiteTag(I('post.title'),$this->sid);
            $this->success('标签"'.I('post.title').'"添加成功');
        }else {
            $this->display();
        }
    }

    public function disable() {
        $id = I('get.id',0,'intval');
        verify($id,'notEmpty','标签ID不能为空');

        $tagInfo = M('SiteTags')->where("sid={$this->sid} and id={$id}")->find($id);;
        verify($tagInfo,'notEmpty','标签不存在');
        
        $result = M('SiteTags')->save(array(
            'id'    =>  $id,
            'status'=>  0,
        ));;
        $this->success('标签《' . $tagInfo['title'] . '》禁用成功');
    }
    
    public function enable() {
        $id = I('get.id',0,'intval');
        verify($id,'notEmpty','标签ID不能为空');

        $tagInfo = M('SiteTags')->where("and sid={$this->sid} and id={$id}")->find($id);;
        verify($tagInfo,'notEmpty','标签不存在');
        
        $result = M('SiteTags')->save(array(
            'id'    =>  $id,
            'status'=>  1,
        ));;
        $this->success('标签《' . $tagInfo['title'] . '》启用成功');
    }

    public function getAjax() {
        $tagid = I('get.id',0,'intval');
        if(!$tagid) {
            $this->errorAjax('标签ID不存在');
        }
        $where = array(
            'tagid' =>  $tagid,
            'sid'   =>  $this->sid,
            'status'=>  1
        );

        $siteTagInfo = D('SiteTags','Mysql')->where($where)->find();
        if(!$siteTagInfo) {
            $this->errorAjax('标签不存在');
        }

        $tagInfo = M('Tags')->find($siteTagInfo['tagid']);

        $this->successAjax($tagInfo);
    
    }
}
?>
